package com.sgb.merchant.view.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;

import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sgb.link.entity.merchant.ApplyDetailEntity;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.sgb.link.router.PathMerchant;
import com.sgb.link.router.PathUser;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantActivityApplyDetailBinding;
import com.sgb.merchant.viewmodel.ApplyDetailViewModel;
import com.swgk.core.base.model.entity.RouterKey;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.FileUtil;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author: 王彬臣
 * @date: 2021/8/26
 * @Description: 报名详情页
 */
@Route(path = PathMerchant.ApplyDetailActivity)
public class ApplyDetailActivity extends BaseActivity<ApplyDetailViewModel, MerchantActivityApplyDetailBinding> {
    @Autowired(name = RouterKey.NO, desc = "项目详情no")
    long no;
    @Autowired(name = RouterKey.ID, desc = "报名详情no")
    String applyNo;

    InvestmentDetailEntity entity;
    ApplyDetailEntity applyDetailEntity;
    private static final String FILE_TYPE_RAR = ".rar";
    private static final String FILE_TYPE_ZIP = ".zip";
    private static final String FILE_TYPE_DOC = ".doc";
    private static final String FILE_TYPE_DOCX = ".docx";
    private static final String FILE_TYPE_PPT = ".ppt";
    private static final String FILE_TYPE_PPTX = ".pptx";
    private static final String FILE_TYPE_XLS = ".xls";
    private static final String FILE_TYPE_XLSX = ".xlsx";
    private static final String FILE_TYPE_PDF = ".pdf";
    private static final String FILE_TYPE_JPG = ".jpg";
    private static final String FILE_TYPE_PNG = ".png";
    private static final String FILE_TYPE_JPEG = ".jpeg";

    @Override
    protected int layoutId() {
        return R.layout.merchant_activity_apply_detail;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(ApplyDetailViewModel.class);
    }

    @SuppressLint("AutoDispose")
    @Override
    protected void init() {
        ARouter.getInstance().inject(this);
        mBinding.setClick(this);
        mBinding.includeTitle.titleView.setText("报名详情");
        mBinding.includeTitle.ivBack.setOnClickListener(v -> {
            finish();
        });
        DialogHelper.showProgressDialog(this, "").setCanceledOnTouchOutside(true);
        mViewModel.getApplyDetail(applyNo);

        mViewModel.ldData.observe(this, data -> {
            entity = data;

        });
        mViewModel.ldApply.observe(this, data -> {
            mBinding.clContent.setVisibility(View.VISIBLE);
            applyDetailEntity = data;
            mBinding.tvProjectName.setText(applyDetailEntity.getItemName());
            mBinding.tvName.setText(applyDetailEntity.getInviteOrgName());
            mBinding.tvType.setText(TextUtils.isEmpty(applyDetailEntity.getItemTypeName()) ? "/" : applyDetailEntity.getItemTypeName());
            mBinding.tvProType.setText(TextUtils.isEmpty(applyDetailEntity.getItemTypeName()) ? "/" : applyDetailEntity.getItemTypeName());
            mBinding.tvPerson.setText(applyDetailEntity.getInviteLinkName());
            mBinding.tvPhone.setText(applyDetailEntity.getInviteLinkPhone() + "");
            mBinding.tvAdress.setText(applyDetailEntity.getItemAddress());
            mBinding.applyName.setText(applyDetailEntity.getApplyOrgName());
            mBinding.linkName.setText(applyDetailEntity.getApplyLinkName());
            mBinding.linkPhone.setText(applyDetailEntity.getApplyLinkPhone() + "");
            mBinding.tvMoney.setText(TextUtils.isEmpty(applyDetailEntity.getItemMoney())?"/":applyDetailEntity.getItemMoney()+"元");
            mBinding.applyMoney.setText(applyDetailEntity.getInvestmentBudget() + "元");
            mBinding.tvApplyAddress.setText(applyDetailEntity.getApplyOrgAddress());
            mBinding.stageName.setText(TextUtils.isEmpty(applyDetailEntity.getStageName())?"/":applyDetailEntity.getStageName());
            mBinding.tvSize.setText(TextUtils.isEmpty(applyDetailEntity.getCapacityScale())?"/":applyDetailEntity.getCapacityScale());
            if(!TextUtils.isEmpty(applyDetailEntity.getStatus())){
                if(applyDetailEntity.getStatus().equals("0")){
                    mBinding.tvStatus.setText("已上架");
                }else{
                    mBinding.tvStatus.setText("已下架");
                }
            }
            if (!TextUtils.isEmpty(applyDetailEntity.getFileName())) {
                mBinding.viewLineGray2.setVisibility(View.VISIBLE);
                mBinding.clFile.setVisibility(View.VISIBLE);
                String fileName = applyDetailEntity.getFileName();
                mBinding.tvFileName.setText(applyDetailEntity.getFileName());
                if (fileName.endsWith(FILE_TYPE_RAR) || fileName.endsWith(FILE_TYPE_ZIP)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_zip);
                } else if (fileName.endsWith(FILE_TYPE_DOC) || fileName.endsWith(FILE_TYPE_DOCX)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_doc);
                } else if (fileName.endsWith(FILE_TYPE_PPT) || fileName.endsWith(FILE_TYPE_PPTX)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_ppt);
                } else if (fileName.endsWith(FILE_TYPE_XLS) || fileName.endsWith(FILE_TYPE_XLSX)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_xlsx);
                } else if (fileName.endsWith(FILE_TYPE_PDF)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_pdf);
                } else if (fileName.endsWith(FILE_TYPE_JPG) || fileName.endsWith(FILE_TYPE_JPEG) || fileName.endsWith(FILE_TYPE_PNG)) {
                    mBinding.image.setImageResource(R.mipmap.merchant_icon_img_type);
                }
                Observable.just(applyDetailEntity.getFileUrl()).
                        map(s -> FileUtil.getFileSize(s)).//子线程里获取文件大小
                        subscribeOn(Schedulers.io()).
                        observeOn(AndroidSchedulers.mainThread()).
                        subscribe(s -> mBinding.tvFileSize.setText(s));
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.file_download) {
            Uri uri = Uri.parse(applyDetailEntity.getFileUrl());
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } else if (v.getId() == R.id.iv_map) {

            ARouter.getInstance().build(PathUser.MapPositionActivity)
                    .withDouble("latitude", Double.valueOf(applyDetailEntity.getApplyLat()))
                    .withDouble("longitude", Double.valueOf(applyDetailEntity.getApplyLng()))
                    .withString("companyName", applyDetailEntity.getInviteOrgName())
                    .withString("address", applyDetailEntity.getItemAddress())
                    .navigation();


        } else if (v.getId() == R.id.iv_address_map) {
            ARouter.getInstance().build(PathUser.MapPositionActivity)
                    .withDouble("latitude", Double.valueOf(applyDetailEntity.getApplyLat()))
                    .withDouble("longitude", Double.valueOf(applyDetailEntity.getApplyLng()))
                    .withString("companyName", applyDetailEntity.getApplyOrgName())
                    .withString("address", applyDetailEntity.getApplyOrgAddress())
                    .navigation();
        }
    }
}
